package com.szosen.web;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.request.PostRequest;
import com.szosen.database.DBService;
import com.szosen.database.UploadRecord;
import com.szosen.json.Archive;
import com.szosen.json.HealthCheck;
import com.szosen.json.OldAssessment;
import com.szosen.json.RootArchive;
import com.szosen.json.RootArchiveDetail;
import com.szosen.json.RootHealthCheck;
import com.szosen.json.RootOldAssessment;
import com.szosen.json.RootQueryArchiveList;
import com.szosen.json.RootQueryVisitPlans;
import com.szosen.json.RootVisit;
import com.szosen.json.RootZhongYiWenJuan;
import com.szosen.json.Visit;
import com.szosen.json.ZhongYiWenJuan;
import com.szosen.util.SharedpreferenceUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

import okhttp3.Call;
import okhttp3.Response;

import static com.szosen.util.SharedpreferenceUtil.getserverAddress;

public class WebService {
    private static final int what_success_ok = 65;
    private static final int what_failed = 77;
    private static final int what_error = 87;
    // 注册请求的账号：aosheng
    // des测试机密钥为：c138d518
    // des正式机密钥为：3f7e2o4d
    // 测试机的访问地址：112.74.82.171:9090
    // 正式机的访问地址：www.dangan580.com
    // uname string 必选 请求注册的账号， 由档案580系统给出
    // verifycode string 必选 “密钥”+“当前时间戳”的MD5值，计算公式为：MD5( password+timestamp)
    // timestamp string 必选 当前时间戳
//	rqpkfk8474wifi密码
//	public static final String main_testpath = "http://172.16.0.136:8090/ZHEHR/*.jsonRequest";
//	public static final String main_path = "http://zhuzihuai.eicp.net/ZHEHR/*.jsonRequest";//测试地址
//	public static final String main_path = "http://geshenjibi.eicp.net/ZHEHR/*.jsonRequest";//我的花生壳地址
//	public static final String main_path = "http://172.16.0.139:8080/ZHEHR/*.jsonRequest";//neiwang
//    public static final String main_path = "http://geshenjibi.eicp.net:15176/ZHEHR/*.jsonRequest";//neiwang
//	public static final String pic_path = "http://zhuzihuai.eicp.net/ZHEHR/*.uploadForm";

    /***
     * 主要的address:port
     */
    public static final String main_path_head = "geshenjibi.eicp.net:15176";
    public static final String http_head = "http://";
    /***
     *  上传表格的路径中的后缀
     */
    public static final String main_path_end = "/ZHEHR/*.jsonRequest";

//    public static final String pic_path = "http://geshenjibi.eicp.net:15176/ZHEHR/*.uploadForm";//neiwang
    /***
     * 上传随访的图片的路径中后缀
     */
    public static final String pic_path_end = "/ZHEHR/*.uploadForm";//neiwang
    public static final String serviceId = "mobileDoctorAppService";
    public static final String serviceAction_login = "login";
    public static final String serviceAction_addVisit = "addVisit";
    public static final String serviceAction_addArchive = "addArchive";
    public static final String serviceAction_updateArchive = "updateArchive";
    public static final String serviceAction_addInvestigation = "addInvestigation";
    public static final String serviceAction_addHealthCheck = "addHealthCheck";
    public static final String serviceAction_addOldAssessment = "addOldAssessment";
    public static final String serviceAction_queryArchiveList = "queryArchiveList";
    public static final String serviceAction_queryVisitPlans = "queryVisitPlans";
    public static final String serviceAction_getArchiveDetail = "getArchiveDetail";
    // 查询网格地址
    public static final String serviceAction_queryGridAddressList = "queryGridAddressList";
    private static final long timeout = 8000L;
    private static final long connTimeOut = 8000L;

    // 高血压、糖尿病随访录入，上传唯一编号后，后台会新增一条数据，后面改为更新数据。

    /***
     * 注册设备的方法
     */
    public static void queryAddress(Context context, StringCallback callback) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        // pr.readTimeOut(timeout);
        // pr.connTimeOut(connTimeOut);
        JSONObject object = new JSONObject();
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            String username = SharedpreferenceUtil.getOnDoctorId(context, "");
            String password = SharedpreferenceUtil.getOnPassword(context, "");
            object.put("username", username);
            object.put("password", password);
            object.put("serviceId", serviceId);
            object.put("serviceAction", "queryGridAddressList");
            // object.put("content", "4404");
            JSONObject data = new JSONObject();
            data.put("content", "4404");
            object.put("data", data);
            // String request =
            // "{'username':'system','password':'bsoft123','serviceId':"
            // + "'mobileAppService','serviceAction':'login'}";
            String request = "{'content':'4404'}";
            // object = JsonUtil.objectToJson(object);
            String s = object.toString();
            System.out.println("json的地址内容：" + s);
            pr.headers("encoding", "utf-8");
            pr.upJson(object);
            // pr.upJson(request);
            pr.execute(callback);
            pr.getRequest();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出错了");
        }
    }

    public static void queryArchiveList(Context context, String idCard,
                                        StringCallback callback) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            RootQueryArchiveList rootQuery = new RootQueryArchiveList();
            rootQuery.setServiceId(serviceId);
            rootQuery.setServiceAction(serviceAction_queryArchiveList);
            rootQuery.setPageNo(1);
            rootQuery.setPageSize(2);
            rootQuery.setContent(idCard);
            String jsonStr = new Gson().toJson(rootQuery);
            System.out.println("json_queryArchiveList的字符串内容：" + jsonStr);
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            pr.execute(callback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void queryVisitPlans(Context context, int pageIndex,
                                       int pageSize, String content, StringCallback callback) {
        PostRequest pr = OkGo.post(getUploadPath(context));
//		pr.readTimeOut(timeout);
//		pr.connTimeOut(connTimeOut);
        try {
            RootQueryVisitPlans rootQuery = new RootQueryVisitPlans();
            rootQuery.setServiceId(serviceId);
            rootQuery.setServiceAction(serviceAction_queryVisitPlans);
            rootQuery.setPageNo(pageIndex);
            rootQuery.setPageSize(pageSize);
            rootQuery.setContent(content);
            String jsonStr = new Gson().toJson(rootQuery);
            System.out.println("queryVisitPlans的字符串内容：" + jsonStr);
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            pr.execute(callback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void getArchiveDetail(Context context, String empiId,
                                        StringCallback callback) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            RootArchiveDetail rootArchiveDetail = new RootArchiveDetail();
            rootArchiveDetail.setServiceId(serviceId);
            rootArchiveDetail.setServiceAction(serviceAction_getArchiveDetail);
            rootArchiveDetail.setPageNo(1);
            rootArchiveDetail.setPageSize(2);
            rootArchiveDetail.setEmpiId(empiId);
            String jsonStr = new Gson().toJson(rootArchiveDetail);
            System.out.println("json_getArchiveDetail单个档案详情的字符串内容：" + jsonStr);
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            pr.execute(callback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 注册设备的方法
     */
    public static void queryMedicines(Context context, StringCallback callback) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        // pr.readTimeOut(timeout);
        // pr.connTimeOut(connTimeOut);
        JSONObject object = new JSONObject();
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            String username = SharedpreferenceUtil.getOnDoctorId(context, "");
            String password = SharedpreferenceUtil.getOnPassword(context, "");
            object.put("username", username);
            object.put("password", password);
            object.put("serviceId", serviceId);
            object.put("serviceAction", "queryMedicines");
            // object.put("content", "4404");
            JSONObject data = new JSONObject();
            data.put("content", "4404");
            object.put("data", data);
            // String request =
            // "{'username':'system','password':'bsoft123','serviceId':"
            // + "'mobileAppService','serviceAction':'login'}";
            String request = "{'content':'4404'}";
            // object = JsonUtil.objectToJson(object);
            String s = object.toString();
            System.out.println("json的地址内容：" + s);
            pr.headers("encoding", "utf-8");
            pr.upJson(object);
            // pr.upJson(request);
            pr.execute(callback);
            pr.getRequest();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出错了");
        }
    }

    /***
     * 注册设备的方法
     */
    public static void login(Context context, StringCallback callback) {
        String serverAddress = SharedpreferenceUtil.getserverAddress(context, main_path_head);
        String result = http_head + serverAddress + main_path_end;
        PostRequest pr = OkGo.post(result);
        // pr.readTimeOut(timeout);
        // pr.connTimeOut(connTimeOut);
        JSONObject object = new JSONObject();
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            // 6210817200019535374
            String username = SharedpreferenceUtil.getOnDoctorId(context, "");
            String password = SharedpreferenceUtil.getOnPassword(context, "");
            object.put("username", username);
            object.put("password", password);
            object.put("serviceId", serviceId);
            object.put("serviceAction", serviceAction_login);
            String request = "{'username':'system','password':'bsoft123','serviceId':"
                    + "'mobileAppService','serviceAction':'login'}";
            // object = JsonUtil.objectToJson(object);
            String s = object.toString();
            System.out.println("json的字符串内容：" + s);
            pr.headers("encoding", "utf-8");
            pr.upJson(object);
            pr.execute(callback);
            pr.getRequest();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出错了");
        }
    }

    private static String getUploadPath(Context context) {
        String username = SharedpreferenceUtil.getOnDoctorId(context, "");
        String token = SharedpreferenceUtil.getToken(context, "");
        String serverAddress = getserverAddress(context, main_path_head);
        String result = http_head + serverAddress + main_path_end + "?";
        if (!TextUtils.isEmpty(username)) {
            result += ("uid=" + username + "&token=" + token);
        }
        System.out.println("访问的路径是：" + result);
        return result;
    }

    private static String getUploadPicPath(Context context) {
        String username = SharedpreferenceUtil.getOnDoctorId(context, "");
        String serverAddress = getserverAddress(context, main_path_head);
        String result = http_head + serverAddress + pic_path_end + "?";
        if (!TextUtils.isEmpty(username)) {
            result += ("uid=" + username);
        }
        System.out.println("访问的图片路径是：" + result);
        return result;
    }

    /***
     * 上传随访表的照片记录
     *
     * @param context
     */
    public static void uploadPicVisit(Context context, String picPath,
                                      StringCallback callback) {
        PostRequest pr = OkGo.post(getUploadPicPath(context));
        // pr.readTimeOut(timeout);
        File file = new File(picPath);
        if (!file.exists()) {
            // 文件不存在
            return;
        }
        pr.params("file1", file);
        pr.execute(callback);

    }

    /***
     * 增加健康档案
     */
    public static void addArchive(Context context, String time,
                                  final Handler jsonHandler, Archive archive) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            // String uid = SharedpreferenceUtil.getOnGovId(context, "");
            // pr.params("uid", username);// 医生的id
            // pr.params("token", token);// 返回的token
            RootArchive rootArchive = new RootArchive();
            rootArchive.setServiceId(serviceId);
            rootArchive.setServiceAction(serviceAction_addArchive);
            rootArchive.setData(archive);
            String jsonStr = new Gson().toJson(rootArchive);
            System.out.println("jsonaddArchive的字符串内容：" + jsonStr);
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            final UploadRecord ur = new UploadRecord(time);
            ur.setType(UploadRecord.Type.JKDA_ADD);
            ur.setJsonStr(jsonStr);
            pr.execute(new StringCallback() {
                @Override
                public void onSuccess(String str, Call arg1, Response arg2) {
                    System.out.println("健康档案返回json：" + str);
                    JSONObject jo;
                    try {
                        jo = new JSONObject(str);
                        int code = jo.optInt("code");
                        String msg = jo.optString("msg");
                        if (code == 200 && "success".equalsIgnoreCase(msg)) {
                            jsonHandler.obtainMessage(what_success_ok, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADOK);
                            ur.setUploadMessage("成功");
                        } else {
                            jsonHandler.obtainMessage(what_failed, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADFAILED);
                            ur.setUploadMessage(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        jsonHandler.obtainMessage(what_failed).sendToTarget();
                        ur.setUploadState(UploadContant.UPLOADFAILED);
                    }
                    DBService.add(ur);
                }

                @Override
                public void onError(Call call1, Response response1,
                                    Exception exception) {
                    super.onError(call1, response1, exception);
                    jsonHandler.obtainMessage(what_error).sendToTarget();
                    ur.setUploadMessage("上传错误");
                    ur.setUploadState(UploadContant.UPLOADFAILED);
                    DBService.add(ur);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 增加健康档案
     */
    public static void addArchive(Context context, String time,
                                  final Handler jsonHandler, String jsonStr) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            // String uid = SharedpreferenceUtil.getOnGovId(context, "");
            // pr.params("uid", username);// 医生的id
            // pr.params("token", token);// 返回的token
            System.out.println("离线数据jsonStr的字符串内容：" + jsonStr);
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            final UploadRecord ur = new UploadRecord(time);
            ur.setType(UploadRecord.Type.JKDA_ADD);
            ur.setJsonStr(jsonStr);
            pr.execute(new StringCallback() {
                @Override
                public void onSuccess(String str, Call arg1, Response response) {
                    System.out.println("健康档案离线数据请求返回json：" + str);
                    JSONObject jo;
                    try {
                        jo = new JSONObject(str);
                        int code = jo.optInt("code");
                        String msg = jo.optString("msg");
                        if (code == 200 && "success".equalsIgnoreCase(msg)) {
                            jsonHandler.obtainMessage(what_success_ok, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADOK);
                            ur.setUploadMessage("成功");
                        } else {
                            jsonHandler.obtainMessage(what_failed, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADFAILED);
                            ur.setUploadMessage(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        jsonHandler.obtainMessage(what_failed).sendToTarget();
                        ur.setUploadState(UploadContant.UPLOADFAILED);
                    }
                    DBService.add(ur);
                }

                @Override
                public void onError(Call call1, Response response1,
                                    Exception exception) {
                    super.onError(call1, response1, exception);
                    jsonHandler.obtainMessage(what_error).sendToTarget();
                    ur.setUploadMessage("上传错误");
                    ur.setUploadState(UploadContant.UPLOADFAILED);
                    DBService.add(ur);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 增加健康档案
     */
    public static void updateArchive(Context context, String time,
                                     final Handler jsonHandler, Archive archive) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            // String username = SharedpreferenceUtil.getOnDoctorId(context,
            // "");
            // String token = SharedpreferenceUtil.getToken(context, "");
            // pr.params("uid", username);// 医生的id
            // pr.params("token", token);// 医生的id
            RootArchive rootArchive = new RootArchive();
            rootArchive.setServiceId(serviceId);
            rootArchive.setServiceAction(serviceAction_updateArchive);
            rootArchive.setData(archive);
            String jsonStr = new Gson().toJson(rootArchive);
            System.out.println("jsonaddArchive的字符串内容：" + jsonStr);
            final UploadRecord ur = new UploadRecord(time);
            ur.setType(UploadRecord.Type.JKDA_UPDATE);
            ur.setJsonStr(jsonStr);
            pr.execute(new StringCallback() {
                @Override
                public void onSuccess(String str, Call arg1, Response arg2) {
                    System.out.println("健康档案update返回json：" + str);
                    if (TextUtils.isEmpty(str)) {
                        // return;
                    }
                    JSONObject jo;
                    try {
                        jo = new JSONObject(str);
                        int code = jo.optInt("code");
                        String msg = jo.optString("msg");
                        if (code == 200 && "success".equalsIgnoreCase(msg)) {
                            jsonHandler.obtainMessage(what_success_ok, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADOK);
                            ur.setUploadMessage("成功");
                        } else {
                            jsonHandler.obtainMessage(what_failed, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADFAILED);
                            ur.setUploadMessage(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        jsonHandler.obtainMessage(what_failed).sendToTarget();
                        ur.setUploadState(UploadContant.UPLOADFAILED);
                    }
                    DBService.add(ur);
                }

                @Override
                public void onError(Call call1, Response response1,
                                    Exception exception) {
                    super.onError(call1, response1, exception);
                    jsonHandler.obtainMessage(what_error).sendToTarget();
                    ur.setUploadMessage("上传错误");
                    ur.setUploadState(UploadContant.UPLOADFAILED);
                    DBService.add(ur);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出错了");
        }
    }

    /***
     * 增加随访录入
     */
    public static void addVisit(Context context, String time,
                                final Handler jsonHandler, Visit visit, String type, String idCard) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            String username = SharedpreferenceUtil.getOnDoctorId(context, "");
            String password = SharedpreferenceUtil.getOnDoctorId(context, "");
            pr.params("uid", username);// 医生的id
            String token = SharedpreferenceUtil.getToken(context, "");
            pr.params("token", token);// 医生的id
            RootVisit rootVisit = new RootVisit();
            rootVisit.setServiceId(serviceId);
            rootVisit.setServiceAction(serviceAction_addVisit);
            rootVisit.setData(visit);
            String jsonStr = new Gson().toJson(rootVisit);
            System.out.println("jsonaddVisit的字符串内容：" + jsonStr);
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            final UploadRecord ur = new UploadRecord(time);
            ur.setType(type);
            ur.setIdCard(idCard);
            ur.setJsonStr(jsonStr);
            pr.execute(new StringCallback() {
                @Override
                public void onSuccess(String str, Call arg1, Response arg2) {
                    System.out.println("addVisit返回json：" + str);
                    JSONObject jo;
                    try {
                        jo = new JSONObject(str);
                        int code = jo.optInt("code");
                        String msg = jo.optString("msg");
                        if (code == 200 && "success".equalsIgnoreCase(msg)) {
                            jsonHandler.obtainMessage(what_success_ok, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADOK);
                            ur.setUploadMessage("成功");
                        } else {
                            jsonHandler.obtainMessage(what_failed, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADFAILED);
                            ur.setUploadMessage(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        jsonHandler.obtainMessage(what_failed).sendToTarget();
                        ur.setUploadState(UploadContant.UPLOADFAILED);
                    }
                    DBService.add(ur);
                }

                @Override
                public void onError(Call call1, Response response1,
                                    Exception exception) {
                    super.onError(call1, response1, exception);
                    jsonHandler.obtainMessage(what_error).sendToTarget();
                    ur.setUploadMessage("上传错误");
                    ur.setUploadState(UploadContant.UPLOADFAILED);
                    DBService.add(ur);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出错了");
        }
    }

    /***
     * 增加随访录入
     */
    public static void addVisit(Context context, String time,
                                final Handler jsonHandler, Visit visit, String type) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            String username = SharedpreferenceUtil.getOnDoctorId(context, "");
            String password = SharedpreferenceUtil.getOnDoctorId(context, "");
            pr.params("uid", username);// 医生的id
            String token = SharedpreferenceUtil.getToken(context, "");
            pr.params("token", token);// 医生的id
            RootVisit rootVisit = new RootVisit();
            rootVisit.setServiceId(serviceId);
            rootVisit.setServiceAction(serviceAction_addVisit);
            rootVisit.setData(visit);
            String jsonStr = new Gson().toJson(rootVisit);
            System.out.println("jsonaddVisit的字符串内容：" + jsonStr);
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            final UploadRecord ur = new UploadRecord(time);
            ur.setType(type);
            ur.setJsonStr(jsonStr);
            pr.execute(new StringCallback() {
                @Override
                public void onSuccess(String str, Call arg1, Response arg2) {
                    System.out.println("addVisit返回json：" + str);
                    JSONObject jo;
                    try {
                        jo = new JSONObject(str);
                        int code = jo.optInt("code");
                        String msg = jo.optString("msg");
                        if (code == 200 && "success".equalsIgnoreCase(msg)) {
                            jsonHandler.obtainMessage(what_success_ok, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADOK);
                            ur.setUploadMessage("成功");
                        } else {
                            jsonHandler.obtainMessage(what_failed, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADFAILED);
                            ur.setUploadMessage(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        jsonHandler.obtainMessage(what_failed).sendToTarget();
                        ur.setUploadState(UploadContant.UPLOADFAILED);
                    }
                    DBService.add(ur);
                }

                @Override
                public void onError(Call call1, Response response1,
                                    Exception exception) {
                    super.onError(call1, response1, exception);
                    jsonHandler.obtainMessage(what_error).sendToTarget();
                    ur.setUploadMessage("上传错误");
                    ur.setUploadState(UploadContant.UPLOADFAILED);
                    DBService.add(ur);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出错了");
        }
    }

    /***
     * 增加随访录入
     */
    public static void addVisit(Context context, String time,
                                final Handler jsonHandler, String jsonStr, String type, String idcard) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            System.out.println("jsonaddVisit的字符串内容：" + jsonStr);
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            final UploadRecord ur = new UploadRecord(time);
            ur.setType(type);
            ur.setJsonStr(jsonStr);
            ur.setIdCard(idcard);
            pr.execute(new StringCallback() {
                @Override
                public void onSuccess(String str, Call arg1, Response arg2) {
                    System.out.println("addVisit离线数据返回json：" + str);
                    JSONObject jo;
                    try {
                        jo = new JSONObject(str);
                        int code = jo.optInt("code");
                        String msg = jo.optString("msg");
                        if (code == 200 && "success".equalsIgnoreCase(msg)) {
                            jsonHandler.obtainMessage(what_success_ok, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADOK);
                            ur.setUploadMessage("成功");
                        } else {
                            jsonHandler.obtainMessage(what_failed, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADFAILED);
                            ur.setUploadMessage(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        jsonHandler.obtainMessage(what_failed).sendToTarget();
                        ur.setUploadState(UploadContant.UPLOADFAILED);
                    }
                    DBService.add(ur);
                }

                @Override
                public void onError(Call call1, Response response1,
                                    Exception exception) {
                    super.onError(call1, response1, exception);
                    jsonHandler.obtainMessage(what_error).sendToTarget();
                    ur.setUploadMessage("上传错误");
                    ur.setUploadState(UploadContant.UPLOADFAILED);
                    DBService.add(ur);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出错了");
        }
    }

    /***
     * 增加健康体检
     */
    public static void addHealthCheck(Context context, String time,
                                      final Handler jsonHandler, HealthCheck healthCheck) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            String username = SharedpreferenceUtil.getOnDoctorId(context, "");
            String password = SharedpreferenceUtil.getOnDoctorId(context, "");
            pr.params("uid", username);// 医生的id
            String token = SharedpreferenceUtil.getToken(context, "");
            pr.params("token", token);// 医生的id
            RootHealthCheck rootHealthCheck = new RootHealthCheck();
            rootHealthCheck.setServiceId(serviceId);
            rootHealthCheck.setServiceAction(serviceAction_addHealthCheck);
            rootHealthCheck.setData(healthCheck);
            String jsonStr = new Gson().toJson(rootHealthCheck);
            int leng2 = jsonStr.length() / 2;
            System.out.println("jsonaddHealthCheck的字符串内容："
                    + jsonStr.substring(0, leng2));
            System.out.println("" + jsonStr.substring(leng2, jsonStr.length()));
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            final UploadRecord ur = new UploadRecord(time);
            ur.setType(UploadRecord.Type.JKTJ);
            ur.setJsonStr(jsonStr);
            pr.execute(new StringCallback() {
                @Override
                public void onSuccess(String str, Call arg1, Response arg2) {
                    System.out.println("addHealthCheck返回json：" + str);
                    JSONObject jo;
                    try {
                        jo = new JSONObject(str);
                        int code = jo.optInt("code");
                        String msg = jo.optString("msg");
                        if (code == 200 && "success".equalsIgnoreCase(msg)) {
                            jsonHandler.obtainMessage(what_success_ok, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADOK);
                            ur.setUploadMessage("成功");
                        } else {
                            jsonHandler.obtainMessage(what_failed, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADFAILED);
                            ur.setUploadMessage(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        jsonHandler.obtainMessage(what_failed).sendToTarget();
                        ur.setUploadState(UploadContant.UPLOADFAILED);
                    }
                    DBService.add(ur);
                }

                @Override
                public void onError(Call call1, Response response1,
                                    Exception exception) {
                    super.onError(call1, response1, exception);
                    jsonHandler.obtainMessage(what_error).sendToTarget();
                    ur.setUploadMessage("上传错误");
                    ur.setUploadState(UploadContant.UPLOADFAILED);
                    DBService.add(ur);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出错了");
        }
    }

    /***
     * 增加健康体检
     */
    public static void addHealthCheck(Context context, String time,
                                      final Handler jsonHandler, String jsonStr) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            int leng2 = jsonStr.length() / 2;
            System.out.println("jsonaddHealthCheck离线数据的字符串内容："
                    + jsonStr.substring(0, leng2));
            System.out.println("" + jsonStr.substring(leng2, jsonStr.length()));
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            final UploadRecord ur = new UploadRecord(time);
            ur.setType(UploadRecord.Type.JKTJ);
            ur.setJsonStr(jsonStr);
            pr.execute(new StringCallback() {
                @Override
                public void onSuccess(String str, Call arg1, Response arg2) {
                    System.out.println("addHealthCheck离线返回json：" + str);
                    JSONObject jo;
                    try {
                        jo = new JSONObject(str);
                        int code = jo.optInt("code");
                        String msg = jo.optString("msg");
                        if (code == 200 && "success".equalsIgnoreCase(msg)) {
                            jsonHandler.obtainMessage(what_success_ok, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADOK);
                            ur.setUploadMessage("成功");
                        } else {
                            jsonHandler.obtainMessage(what_failed, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADFAILED);
                            ur.setUploadMessage(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        jsonHandler.obtainMessage(what_failed).sendToTarget();
                        ur.setUploadState(UploadContant.UPLOADFAILED);
                    }
                    DBService.add(ur);
                }

                @Override
                public void onError(Call call1, Response response1,
                                    Exception exception) {
                    super.onError(call1, response1, exception);
                    jsonHandler.obtainMessage(what_error).sendToTarget();
                    ur.setUploadMessage("上传错误");
                    ur.setUploadState(UploadContant.UPLOADFAILED);
                    DBService.add(ur);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出错了");
        }
    }

    /***
     * 增加老年人生活自理能力
     */
    public static void addOldAssessment(Context context, String time,
                                        final Handler jsonHandler, OldAssessment oldAssessment) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            String username = SharedpreferenceUtil.getOnDoctorId(context, "");
            String password = SharedpreferenceUtil.getOnDoctorId(context, "");
            pr.params("uid", username);// 医生的id
            String token = SharedpreferenceUtil.getToken(context, "");
            pr.params("token", token);// 医生的id
            RootOldAssessment rootOldAssessment = new RootOldAssessment();
            rootOldAssessment.setServiceId(serviceId);
            rootOldAssessment.setServiceAction(serviceAction_addOldAssessment);
            rootOldAssessment.setData(oldAssessment);
            String jsonStr = new Gson().toJson(rootOldAssessment);
            System.out.println("jsonaddOldAssessment的字符串内容：" + jsonStr);
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            final UploadRecord ur = new UploadRecord(time);
            ur.setType(UploadRecord.Type.SHZLNL);
            ur.setJsonStr(jsonStr);
            pr.execute(new StringCallback() {
                @Override
                public void onSuccess(String str, Call arg1, Response arg2) {
                    System.out.println("addVisit返回json：" + str);
                    JSONObject jo;
                    try {
                        jo = new JSONObject(str);
                        int code = jo.optInt("code");
                        String msg = jo.optString("msg");
                        if (code == 200 && "success".equalsIgnoreCase(msg)) {
                            jsonHandler.obtainMessage(what_success_ok, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADOK);
                            ur.setUploadMessage("成功");
                        } else {
                            jsonHandler.obtainMessage(what_failed, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADFAILED);
                            ur.setUploadMessage(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        jsonHandler.obtainMessage(what_failed).sendToTarget();
                        ur.setUploadState(UploadContant.UPLOADFAILED);
                    }
                    DBService.add(ur);
                }

                @Override
                public void onError(Call call1, Response response1,
                                    Exception exception) {
                    super.onError(call1, response1, exception);
                    jsonHandler.obtainMessage(what_error).sendToTarget();
                    ur.setUploadMessage("上传错误");
                    ur.setUploadState(UploadContant.UPLOADFAILED);
                    DBService.add(ur);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出错了");
        }
    }

    /***
     * 增加老年人生活自理能力
     */
    public static void addOldAssessment(Context context, String time,
                                        final Handler jsonHandler, String jsonStr) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            System.out.println("jsonaddOldAssessment离线的字符串内容：" + jsonStr);
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            final UploadRecord ur = new UploadRecord(time);
            ur.setType(UploadRecord.Type.SHZLNL);
            ur.setJsonStr(jsonStr);
            pr.execute(new StringCallback() {
                @Override
                public void onSuccess(String str, Call arg1, Response arg2) {
                    System.out.println("addVisit返回json：" + str);
                    JSONObject jo;
                    try {
                        jo = new JSONObject(str);
                        int code = jo.optInt("code");
                        String msg = jo.optString("msg");
                        if (code == 200 && "success".equalsIgnoreCase(msg)) {
                            jsonHandler.obtainMessage(what_success_ok, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADOK);
                            ur.setUploadMessage("成功");
                        } else {
                            jsonHandler.obtainMessage(what_failed, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADFAILED);
                            ur.setUploadMessage(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        jsonHandler.obtainMessage(what_failed).sendToTarget();
                        ur.setUploadState(UploadContant.UPLOADFAILED);
                    }
                    DBService.add(ur);
                }

                @Override
                public void onError(Call call1, Response response1,
                                    Exception exception) {
                    super.onError(call1, response1, exception);
                    jsonHandler.obtainMessage(what_error).sendToTarget();
                    ur.setUploadMessage("上传错误");
                    ur.setUploadState(UploadContant.UPLOADFAILED);
                    DBService.add(ur);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("出错了");
        }
    }

    /***
     * 增加老年人中医药管理
     */
    public static void addZhongYiWenJuan(Context context, String time,
                                         final Handler jsonHandler, ZhongYiWenJuan zhongYiWenJuan) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            String username = SharedpreferenceUtil.getOnDoctorId(context, "");
            String password = SharedpreferenceUtil.getOnDoctorId(context, "");
            pr.params("uid", username);// 医生的id
            String token = SharedpreferenceUtil.getToken(context, "");
            pr.params("token", token);// 医生的id
            RootZhongYiWenJuan root = new RootZhongYiWenJuan();
            root.setServiceId(serviceId);
            root.setServiceAction(serviceAction_addInvestigation);
            root.setData(zhongYiWenJuan);
            String jsonStr = new Gson().toJson(root);
            System.out.println("jsonaddZhongYiWenJuan的字符串内容：" + jsonStr);
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            final UploadRecord ur = new UploadRecord(time);
            ur.setType(UploadRecord.Type.LNRZYY);
            ur.setJsonStr(jsonStr);
            pr.execute(new StringCallback() {
                @Override
                public void onSuccess(String str, Call arg1, Response arg2) {
                    System.out.println("addVisit返回json：" + str);
                    JSONObject jo;
                    try {
                        jo = new JSONObject(str);
                        int code = jo.optInt("code");
                        String msg = jo.optString("msg");
                        if (code == 200 && "success".equalsIgnoreCase(msg)) {
                            jsonHandler.obtainMessage(what_success_ok, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADOK);
                            ur.setUploadMessage("成功");
                        } else {
                            jsonHandler.obtainMessage(what_failed, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADFAILED);
                            ur.setUploadMessage(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        jsonHandler.obtainMessage(what_failed).sendToTarget();
                        ur.setUploadState(UploadContant.UPLOADFAILED);
                    }
                    DBService.add(ur);
                }

                @Override
                public void onError(Call call1, Response response1,
                                    Exception exception) {
                    super.onError(call1, response1, exception);
                    jsonHandler.obtainMessage(what_error).sendToTarget();
                    ur.setUploadMessage("上传错误");
                    ur.setUploadState(UploadContant.UPLOADFAILED);
                    DBService.add(ur);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /***
     * 增加老年人中医药管理
     */
    public static void addZhongYiWenJuan(Context context, String time,
                                         final Handler jsonHandler, String jsonStr) {
        PostRequest pr = OkGo.post(getUploadPath(context));
        pr.readTimeOut(timeout);
        pr.connTimeOut(connTimeOut);
        try {
            // object.put("username", "system");
            // object.put("password", "bsoft123");
            System.out.println("jsonaddZhongYiWenJuan离线的字符串内容：" + jsonStr);
            pr.headers("encoding", "utf-8");
            pr.upJson(jsonStr);
            final UploadRecord ur = new UploadRecord(time);
            ur.setType(UploadRecord.Type.LNRZYY);
            ur.setJsonStr(jsonStr);
            pr.execute(new StringCallback() {
                @Override
                public void onSuccess(String str, Call arg1, Response arg2) {
                    System.out.println("add中医问卷离线返回json：" + str);
                    JSONObject jo;
                    try {
                        jo = new JSONObject(str);
                        int code = jo.optInt("code");
                        String msg = jo.optString("msg");
                        if (code == 200 && "success".equalsIgnoreCase(msg)) {
                            jsonHandler.obtainMessage(what_success_ok, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADOK);
                            ur.setUploadMessage("成功");
                        } else {
                            jsonHandler.obtainMessage(what_failed, msg)
                                    .sendToTarget();
                            ur.setUploadState(UploadContant.UPLOADFAILED);
                            ur.setUploadMessage(msg);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        jsonHandler.obtainMessage(what_failed).sendToTarget();
                        ur.setUploadState(UploadContant.UPLOADFAILED);
                    }
                    DBService.add(ur);
                }

                @Override
                public void onError(Call call1, Response response1,
                                    Exception exception) {
                    super.onError(call1, response1, exception);
                    jsonHandler.obtainMessage(what_error).sendToTarget();
                    ur.setUploadMessage("上传错误");
                    ur.setUploadState(UploadContant.UPLOADFAILED);
                    DBService.add(ur);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
